package com.core.shared.file;

import com.core.model.lxf.FileUpload;
//import it.sauronsoftware.jave.Encoder;
//import it.sauronsoftware.jave.EncoderException;
//import it.sauronsoftware.jave.InputFormatException;
//import it.sauronsoftware.jave.MultimediaInfo;
//import javazoom.jl.decoder.Bitstream;
//import javazoom.jl.decoder.BitstreamException;
//import javazoom.jl.decoder.Header;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-8-13
 * Time: 上午11:04
 * To change this template use File | Settings | File Templates.
 */
public class FileHandle {

    public static FileUpload toFile(MultipartFile multipartFile) {
        FileUpload file = null;
        try {
            String name = multipartFile.getOriginalFilename();
            String type = multipartFile.getContentType();
            byte[] bytes = multipartFile.getBytes();
            file = new FileUpload(name, type, bytes);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return file;
    }

    public static int getMp3Time(String file) {
        FileInputStream fis;
        int time = 0;
//        try {
//            fis = new FileInputStream(new File(file));
//            int b = fis.available();
//            Bitstream bt = new Bitstream(fis);
//            Header h = bt.readFrame();
//            time = (int) h.total_ms(b);
//        } catch (FileNotFoundException e) {
//            e.printStackTrace();
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (BitstreamException e) {
//            e.printStackTrace();
//        }
        int i = time / 1000; //s
        System.out.println("mp3 time: " + i + "s");
        return i;
    }

//    public static long getMp3TimeNew(String fileDir) {  // can use
//        File source = new File(fileDir);
//        Encoder encoder = new Encoder();
//        long t = 1;
//        try {
//            MultimediaInfo m = encoder.getInfo(source);
//            t = m.getDuration() / 1000;
//        } catch (InputFormatException e) {
//            e.printStackTrace();
//        } catch (EncoderException e) {
//            e.printStackTrace();
//        }
//        return t;
//    }

    public static boolean createMenu(String path) {
        File file = new File(path);
        if (!file.exists()) {
            if (file.mkdirs()) {
                return true;
            } else {
                return false;
            }
        } else {
            return true;
        }
    }

}
